﻿using System;
using System.Collections.Generic;
using System.Text;
using ElteIk.RoboSoccer.Core;
using ElteIk.RoboSoccer.Communication;
using ElteIk.RoboSoccer.Core.Physics;

namespace ElteIk.RoboSoccer.DavidTactics
{
    public class DavidTactics : ITactics
    {
        private const int gX = 10;
        private const int gY = 10;

        private int teamId;
        private RoboSoccerGame game;
        private RoboSoccerTeam team;
        private RoboSoccerTeam enemy;
        private SoccerField field;
        private ActionList[] robotActions;

        private static AStar aStar;
        internal static AStar PathFinder
        {
            get { return aStar; }
        }

        private static EngineDirector director;
        internal static EngineDirector Director
        {
            get { return director; }
        }

        public DavidTactics(int teamId)
        {
            this.teamId = teamId;
        }

        private void InitializeTactics(RoboSoccerGame game, int teamId)
        {
            this.game = game;
            team = teamId == 1 ? game.LeftTeam : game.RightTeam;
            enemy = teamId == 1 ? game.RightTeam : game.LeftTeam;
            field = game.Field;
            aStar = new AStar(field, team, enemy, gX, gY);
            director = new EngineDirector();
            robotActions = new ActionList[team.Robots.Count];
            for (int i = 0; i < team.Robots.Count; ++i)
                robotActions[i] = new ActionList();
        }

        public RoboSoccerGame Game
        {
            get { return game; }
            set
            {
                game = value;
                InitializeTactics(game, teamId);
            }
        }

        private int frame = 0;

        public void Do()
        {
            if (game == null)
                return;

            if (frame == 2)
            {
                robotActions[0].Add(
                    new PathfindingAction(team.Robots[0], new Vector2D(100.0f, 100.0f)));
            }
            ++frame;

            aStar.NewFrame();
            for (int i = 0; i < team.Robots.Count; ++i)
                robotActions[i].Do();
        }
    }
}